package com.bigdata.movieticketapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.entity.MovieOrder;
import com.bigdata.movieticketapi.entity.OrderInfo;
import com.bigdata.movieticketapi.entity.User;
import com.bigdata.movieticketapi.mapper.MovieOrderMapper;
import com.bigdata.movieticketapi.mapper.UserMapper;
import com.bigdata.movieticketapi.service.IMovieOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.service.IOrderInfoService;
import com.bigdata.movieticketapi.vo.req.CancelOrderReqVO;
import com.bigdata.movieticketapi.vo.req.DoPayReqVO;
import com.bigdata.movieticketapi.vo.req.GetDataByIdReqVo;
import com.bigdata.movieticketapi.vo.req.PageMovieOrderReqVO;
import com.bigdata.movieticketapi.vo.resp.PageMovieOrderRespVO;
import com.bigdata.movieticketapi.vo.resp.SoldMovieSeatRespVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-11-08
 */
@Service
public class MovieOrderServiceImpl extends ServiceImpl<MovieOrderMapper, MovieOrder> implements IMovieOrderService {

    @Resource
    private MovieOrderMapper movieOrderMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private IOrderInfoService iOrderInfoService;

    @Override
    public void updateMovieOrder(MovieOrder movieOrder) {
        int result = movieOrderMapper.updateById(movieOrder);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageMovieOrderRespVO> pageMovieOrders(PageMovieOrderReqVO vo) {
        IPage<PageMovieOrderRespVO> result = movieOrderMapper.selectPageOrders(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(), result.getTotal());
    }

    @Override
    public List<SoldMovieSeatRespVO> selectSoldMovieSeat(GetDataByIdReqVo vo) {
        return movieOrderMapper.selectSoldMovieSeat(vo);
    }

    @Transactional
    @Override
    public void doPay(DoPayReqVO vo) {
        MovieOrder movieOrder = new MovieOrder();
        movieOrder.setUserId(vo.getUserId());
        movieOrder.setMovieSession(vo.getMovieSession());
        movieOrder.setCreateTime(LocalDateTime.now());
        movieOrder.setIsCancel(0);
        movieOrder.setIsComplete(1);
        int insert = movieOrderMapper.insert(movieOrder);
        if(insert != 1){
            throw new BusinessException(ResponseCode.DO_PAY_EXISTS.getCode(),ResponseCode.DO_PAY_EXISTS.getMessage());
        }
        List<OrderInfo> orderInfoList = new ArrayList<>();
        vo.getSeatList().forEach(baseSeat -> {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setOrderId(movieOrder.getId());
            orderInfo.setRowCode(baseSeat.getRowCode());
            orderInfo.setColumnCode(baseSeat.getColumnCode());
            orderInfoList.add(orderInfo);
        });
        boolean saved = iOrderInfoService.saveBatch(orderInfoList);
        if(!saved){
            throw new BusinessException(ResponseCode.DO_PAY_EXISTS.getCode(),ResponseCode.DO_PAY_EXISTS.getMessage());
        }
        User user = userMapper.selectById(vo.getUserId());
        BigDecimal wallet = user.getWallet();
        BigDecimal balance = wallet.subtract(vo.getTotalPrice());
        user.setWallet(balance);
        int update = userMapper.updateById(user);
        if(update!=1){
            throw new BusinessException(ResponseCode.DO_PAY_EXISTS.getCode(),ResponseCode.DO_PAY_EXISTS.getMessage());
        }


    }
}
